package com.example.androidprogressbar.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.example.androidprogressbar.R;

public class DottedLineProgressBar extends View {

    private Paint mPaint;
    private Paint mPaintBack;
    private Paint mPaintText;
    private Paint mPaintTextLevel;
    private int strokeWidth = 30;
    private int textSize = 22;
    private int textSizeBlow = 15;
    private long duration = 3500;
    private int process;

    public DottedLineProgressBar(Context context) {
        super(context);
        init();
    }

    public DottedLineProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DottedLineProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setStrokeWidth(int width) {
        strokeWidth = width;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public void setTextSizeBlow(int textSizeBlow) {
        this.textSizeBlow = textSizeBlow;
    }

    //初始化画笔
    private void init() {
        mPaintBack = new Paint();//圆角矩形
        mPaintBack.setColor(getResources().getColor(R.color.gray));//圆角矩形颜色
        mPaintBack.setAntiAlias(true);// 抗锯齿效果
        mPaintBack.setStyle(Paint.Style.STROKE);//设置画笔样式
        mPaintBack.setStrokeWidth(strokeWidth);//设置画笔宽度

        mPaint = new Paint();
        mPaint.setColor(getResources().getColor(R.color.blue));
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(strokeWidth);

        mPaintText = new Paint();
        mPaintText.setAntiAlias(true);
        mPaintText.setStyle(Paint.Style.FILL);
        mPaintText.setColor(getResources().getColor(R.color.blue));
        mPaintText.setTextSize(sp2px(textSize));

        mPaintTextLevel = new Paint();
        mPaintTextLevel.setAntiAlias(true);
        mPaintTextLevel.setStyle(Paint.Style.FILL);
        mPaintTextLevel.setColor(getResources().getColor(R.color.gray));
        mPaintTextLevel.setTextSize(sp2px(textSizeBlow));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBack(canvas);
        drawProgress(canvas);
        drawText(canvas);
        drawTextBlow(canvas);
    }

    private void drawBack(Canvas canvas) {
        RectF rectF = new RectF(strokeWidth, strokeWidth, getWidth() - strokeWidth, getHeight() - strokeWidth);
        PathEffect effects = new DashPathEffect(new float[]{8, 6}, 0);
        mPaintBack.setPathEffect(effects);
        canvas.drawArc(rectF, 0, 360, false, mPaintBack);
    }

    private void drawProgress(Canvas canvas) {
        RectF rectF = new RectF(strokeWidth, strokeWidth, getWidth() - strokeWidth, getHeight() - strokeWidth);
        PathEffect effects = new DashPathEffect(new float[]{8, 6}, 0);
        mPaint.setPathEffect(effects);
        canvas.drawArc(rectF, 0, process, false, mPaint);
    }

    //绘制文字
    private void drawText(Canvas canvas) {
        int mTxtWidth = getTextWidth();
        int mTxtHeight = getTextHeight();
        int x = getWidth() / 2 - mTxtWidth / 2;
        int y = getHeight() / 2 + mTxtHeight / 4;
        canvas.drawText(getResources().getString(R.string.defaultTextEmui), x, y, mPaintText);
    }

    //绘制下方文字
    private void drawTextBlow(Canvas canvas) {
        int mTxtWidth = getTextWidthBlow();
        int mTxtHeight = getTextHeight();
        int x = getWidth() / 2 - mTxtWidth / 2;
        int y = getHeight() / 2 + mTxtHeight / 4 + 55;
        canvas.drawText(getResources().getString(R.string.defaultTextBelow), x, y, mPaintTextLevel);
    }

    private int getTextWidth() {
        String text = getResources().getString(R.string.defaultTextEmui);
        return (int) mPaintText.measureText(text, 0, text.length());
    }

    private int getTextWidthBlow() {
        String text = getResources().getString(R.string.defaultTextBelow);
        return (int) mPaintTextLevel.measureText(text, 0, text.length());
    }

    private int getTextHeight() {
        Paint.FontMetrics fm = mPaintText.getFontMetrics();
        return (int) Math.ceil(fm.descent - fm.ascent);
    }

    private int sp2px(int sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
                getResources().getDisplayMetrics());
    }

    /**
     * 设置动画效果
     */
    public void start() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);
        valueAnimator.setDuration(duration);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(animation -> {
            process = (int) animation.getAnimatedValue();
            invalidate();
        });
        valueAnimator.start();
    }
}
